import java.awt.*;
import java.awt.event.*;
import javax.swing.*;


public class CustomComboBoxDemo extends JPanel {
    ImageIcon[] images;
    String[] petStrings = {"dostepny","niedostepny"};
    static boolean status;
    public CustomComboBoxDemo() {
        super(new BorderLayout());
        
        //ladowanie ikon i tworzenie tablicy ikon
        images = new ImageIcon[petStrings.length];
        Integer[] intArray = new Integer[petStrings.length];
        for (int i = 0; i < petStrings.length; i++) {
            intArray[i] = new Integer(i);
            images[i] = createImageIcon("icon/" + petStrings[i] + ".png");
        }

        //tworzenie listy rozwijanej
        JComboBox petList = new JComboBox(intArray);
        ComboBoxRenderer renderer= new ComboBoxRenderer();
        renderer.setPreferredSize(new Dimension(200, 40));
        petList.setRenderer(renderer);
        petList.setMaximumRowCount(2);
        add(petList, BorderLayout.PAGE_START);

    }

    /** Pokazywanie ikon, w przeciwnym razie napis */
    protected static ImageIcon createImageIcon(String path) {
        java.net.URL imgURL = CustomComboBoxDemo.class.getResource(path);
        if (imgURL != null) {
            return new ImageIcon(imgURL);
        } else {
            System.err.println("nie moge znalesc ikony: " + path);
                return null;
        }
    }

    class ComboBoxRenderer extends JLabel
                           implements ListCellRenderer {
        private Font uhOhFont;

        public ComboBoxRenderer() {
            setOpaque(true);
            setHorizontalAlignment(CENTER);
            setVerticalAlignment(CENTER);
        }

        /*
         * metoda znajduje ikone i podstawia do zaznaczenia
         */
        public Component getListCellRendererComponent(
                                           JList list,
                                           Object value,
                                           int index,
                                           boolean isSelected,
                                           boolean cellHasFocus) {
            //Get the selected index. (The index param isn't
            //always valid, so just use the value.)
            int selectedIndex = ((Integer)value).intValue();

            if (isSelected) {
              setBackground(list.getSelectionBackground());
              // setForeground(list.getSelectionForeground());


            } else {
               setBackground(list.getBackground());
               // setForeground(list.getForeground());

            }

           
            //Set the icon and text.  If icon was null, say so.
            ImageIcon icon = images[selectedIndex];
            String pet = petStrings[selectedIndex];

            setIcon(icon);

            if (selectedIndex==0){status=true; }
            else {status=false;}
            return this;
        }


    }
}
